package com.leokok.jts.learning.jts.core.algorithm;

import org.junit.Test;
import org.locationtech.jts.algorithm.Length;
import org.locationtech.jts.geom.CoordinateSequence;
import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.geom.impl.CoordinateArraySequence;
import org.locationtech.jts.geom.impl.CoordinateArraySequenceFactory;
import org.locationtech.jts.io.ParseException;
import org.locationtech.jts.io.WKTReader;

/**
 * 通过遍历每个coordinate计算距离
 */
public class LengthTest {

    @Test
    public void length() throws ParseException {
        WKTReader wktReader = new WKTReader();
        Geometry geometry = wktReader.read("LINESTRING (115.9234 31.99, 115.9243 31.9898, 115.9266 31.9896, 115.92673333333333 31.9896, 115.9299 31.9895, 115.93 31.989466666666665, 115.9314 31.9894, 115.9315 31.989366666666665, 115.93163333333334 31.989333333333335, 115.9339 31.9892, 115.937 31.989)");

        CoordinateSequence coordinateSequence = CoordinateArraySequenceFactory.instance().create(geometry.getCoordinates());

       /* CoordinateArraySequence coordinateArraySequence = new CoordinateArraySequence(geometry.getCoordinates());*/
        Double length = Length.ofLine(coordinateSequence);
        System.out.println(length);
    }
}
